#!/bin/sh
#
# DracoPKG
#
# Copyright 2008 Ole Andre Rodlie <olear@dracolinux.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function dP_suspend () {

# Copyright Jean-Philippe Guillemin <jp.guillemin@free.fr>. This program is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your option)
# any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm

# Modified for Draco GNU/Linux by Ole Andre Rodlie <olear@slackforge.net>

tmpfile=$(mktemp /tmp/video_state.XXXXXX)
trap 'rm -f $tmpfile' 0 1 15

vid=$(lspci | sed -e '/VGA/!d' -e 's/ .*//' -e 's@0000:@@' -e 's@:@/@' -eq)
procvideoid="/proc/bus/pci/$vid"

if [ ! -f /etc/hibernate.modules ]; then
  touch /etc/hibernate.modules
fi

unloadlist="/etc/hibernate.modules"

for module in $(/sbin/lsmod | sed -n 's/^\([^ \t]*\)[ \t]*[0-9]\+[ \t]*[0-9]*.*[ \t]*$/\1/p' | sort) ; do
   echo $module | egrep -q -f $unloadlist && modules="${modules} ${module}"
done

if [ "$(grep "standby" /sys/power/state)" ] ; then
  if [ -x /etc/rc.d/rc.oss ]; then
    /etc/rc.d/rc.oss stop
  fi
  for module in $modules ; do
    /sbin/rmmod -f $module
  done   
  sync
  /sbin/hwclock --systohc
  cat $procvideoid > $tmpfile      
  echo -n standby > /sys/power/state   
  cat $tmpfile > $procvideoid
  /sbin/hwclock --hctosys
  for module in $modules ; do
    /sbin/modprobe -f $module
  done
  if [ -x /etc/rc.d/rc.oss ]; then
    /etc/rc.d/rc.oss start
  fi
  rm -f $tmpfile   
  exit         
elif [ "$(grep "mem" /sys/power/state)" ] ; then
  if [ -x /etc/rc.d/rc.oss ]; then
    /etc/rc.d/rc.oss stop
  fi
  for module in $modules ; do
    /sbin/rmmod -f $module
  done   
  sync
  /sbin/hwclock --systohc
  cat $procvideoid > $tmpfile
  echo -n mem > /sys/power/state
  cat $tmpfile > $procvideoid
  /sbin/hwclock --hctosys
  for module in $modules ; do
    /sbin/modprobe -f $module
  done
  if [ -x /etc/rc.d/rc.oss ]; then
    /etc/rc.d/rc.oss start
  fi
  rm -f $tmpfile
  exit
else
  echo "No standby or suspend to memory state available, exiting"
  exit
fi
}
